package com.erp.pur.controller;

import java.util.*;

import com.erp.common.controller.BaseController;
import com.erp.common.utils.*;
import com.erp.itm.domain.ProjectDO;
import com.erp.itm.service.ProjectService;
import com.erp.oa.domain.NotifyDO;
import com.erp.oa.service.NotifyService;
import com.erp.pro.domain.InstockDO;
import com.erp.system.domain.UserDO;
import com.erp.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.pur.domain.ApplyDO;
import com.erp.pur.service.ApplyService;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-11-09 14:44:41
 */
 
@Controller
@RequestMapping("/pur/apply")
public class ApplyController extends BaseController {
	@Autowired
	private ApplyService applyService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserService userService;

	@Autowired
	private NotifyService notifyService;
	
	@GetMapping()
	String Apply(){
	    return "pur/apply/apply";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ApplyDO> applyList = applyService.list(query);
		int total = applyService.count(query);
		PageUtils pageUtils = new PageUtils(applyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "pur/apply/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		ApplyDO apply = applyService.get(id);
		model.addAttribute("apply", apply);
		//查询项目信息
		List<ProjectDO> projectDOList = projectService.getNotEndProjectlist();
		model.addAttribute("projectDOList", projectDOList);
	    return "pur/apply/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( ApplyDO apply){
		//查询项目信息
		if(apply.getProjectId() != null){
			ProjectDO projectDO = projectService.get(apply.getProjectId());
			if(projectDO != null){
				apply.setProjectItmnumber(projectDO.getItmNumber());
				apply.setProjectName(projectDO.getName());
			}
		}
		apply.setStatus(1);
		UserDO userDO = ShiroUtils.getUser();
		apply.setCreateTime(new Date());
		apply.setCreateUserid(userDO.getUserId());
		apply.setCreateUsername(userDO.getName());
		if(applyService.save(apply)>0){
			//发通知和邮件通知采购和总经理
			NotifyDO notify = new NotifyDO();
			notify.setType("4");
			notify.setTitle("采购申请");
			notify.setContent(userDO.getName()+"申请采购项目编号["+apply.getProjectItmnumber()+"]的商品 /n"+
					apply.getPurDescr()+" /n具体内容请去采购申请中查看");
			//查询采购和总经理用户
			List<Long> deprids = new ArrayList<>();
			deprids.add(16L);
			deprids.add(17L);
//			deprids.add(6L);
			List<UserDO> userDOList =userService.getUsersByDeptIds(deprids);
			if(userDOList != null && userDOList.size()>0){
				Long[] userIds = new Long[userDOList.size()];
				for (int i= 0;i<userDOList.size();i++){
					userIds[i] = userDOList.get(i).getUserId();
				}
				notify.setUserIds(userIds);
			}
			notify.setStatus("1");
			notify.setCreateBy(getUserId());
			notifyService.save(notify);
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( ApplyDO apply){
		//查询项目信息
		if(apply.getProjectId() != null){
			ProjectDO projectDO = projectService.get(apply.getProjectId());
			if(projectDO != null){
				apply.setProjectItmnumber(projectDO.getItmNumber());
				apply.setProjectName(projectDO.getName());
			}
		}
		applyService.update(apply);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(applyService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		applyService.batchRemove(ids);
		return R.ok();
	}
	/**
	 * 确认采购
	 */
	@PostMapping( "/comfirm")
	@ResponseBody
	public R comfirm( Long id){
		if(applyService.comfirm(id)>0){
			return R.ok();
		}
		return R.error();
	}



	@PostMapping("/exportData")
	@ResponseBody
	public void downloadTemplate(ApplyDO apply, HttpServletResponse response){
		//ProjectDO project
		Map<String, Object> params = new HashMap<>();
		if(StringUtils.isNotBlank(apply.getCreateUsername())){
			params.put("createUsername",apply.getCreateUsername());
		}
		if(StringUtils.isNotBlank(apply.getProjectItmnumber())){
			params.put("projectItmnumber",apply.getProjectItmnumber());
		}
		if(apply.getPurTime() != null){
			params.put("purTime",apply.getPurTime());
		}
		List<ApplyDO> applyDOList = applyService.list(params);
		ExcelUtil.exportExcel(applyDOList,"采购申请","采购申请",ApplyDO.class,"采购申请.xls",response);
	}
}
